﻿using B.Auth.APi.Application.Command.Leaves;
using B.Auth.APi.Application.Command.Leaves.LeaveDto;
using B.Auth.APi.Application.Command.Menus;
using B.Auth.APi.Application.Command.Menus.Dto;
using B.Auth.APi.Application.Command.Role;
using B.Auth.APi.Application.Command.Role.DTO;
using B.Auth.APi.Application.Command.RoleMenus;
using B.Auth.APi.Application.Command.RoleUsers;
using B.Auth.APi.Application.Command.RoleUsers.Dto;
using B.Auth.APi.Application.Command.Users;
using B.Auth.APi.Application.Command.Users.Dtos;
using B.Auth.Domain.Entity;
using B.Auth.Enum;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace B.Auth.APi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {

        private readonly IMediator mediator;
        private readonly ILogger<ManagementController> logger;

        public ManagementController(IMediator mediator, ILogger<ManagementController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        [AllowAnonymous]
        public Task<ApiResult<LoginDto>> Logine([FromQuery]LoginCommand request)
        {
            try
            {
                logger.LogInformation("登录成功");
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
           
            
        }

        /// <summary>
        /// 报表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<List<LeaveReportDto>>>> GetReposit([FromQuery]LeaveReprotCommand request)
        {

            var usernae = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;

            return mediator.Send(request);
        }

        /// <summary>
        /// 根据部门id获取用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetUserTreeDto>>> GetUserNameTree([FromQuery]GetUserNameTreeCommand request)
        {
            return await mediator.Send(request);

        }


        /// <summary>
        /// 获取领导名称（只适用单人）
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<List<string>>> GetBoosName()
        {

            var request = new GetBoosNameCommand();
            return mediator.Send(request);
        }


        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> Shen(LeaveShenSaveCommand request)
        {

            return mediator.Send(request);
        }


        /// <summary>
        /// 修改boos状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> Updataboos(updataBoosRoleCommand request)
        { 

            return mediator.Send(request);
        
        }
        /// <summary>
        /// 请假信息删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> LeaveDel(LeaveDelCommand request)
        {

            return mediator.Send(request);
        }

        /// <summary>
        /// 请假信息修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>


        [HttpPost]
        public Task<ApiResult<int>> Updateleave(LeaveUpdataCommand request)
        {

            return mediator.Send(request);
        }

        /// <summary>
        /// 请假申请
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpPost]
        public Task<ApiResult<int>> AddLeave(LeaveAddCommand request)
        {
            return mediator.Send(request);
        
        }

        /// <summary>
        /// 获取请假列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<List<LeaveshowDto>>>> GetLeaveshow([FromQuery]GetLeaveshowCommand request)
        {

           

            return mediator.Send(request);
        
        }


        /// <summary>
        /// 根据用户id查角色id,再查菜单id，在根据菜单id条件查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<MenuModel>>>GetRoleUserRoleid([FromQuery]GetRoleidCommand request)
        {

            return mediator.Send(request);
        }


        /// <summary>
        /// 根据角色id查询菜单id数组
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public Task<ApiResult<List<int>>> Getmenuid([FromQuery]GetMenuIdcommand request)
        {

            return mediator.Send(request);

        }

        /// <summary>
        /// 菜单树形
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MenusTreeDto>>> GetMenuTree()
        {

            var resquest = new MenusTreeCommand();
            return await mediator.Send(resquest);

        }
        /// <summary>
        /// 角色树形下拉
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<RoleTreeDto>>> GetRoleTree()
        {
            var request = new RoleTreeCommand();

            return mediator.Send(request);
        }

        /// <summary>
        /// 菜单表信息查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<MenuDto>>> GetMenus()
        {
            var request = new MenusShowCommand();
            return mediator.Send(request);
        }


        /// <summary>
        /// 根据角色id获取用户Id集合
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<int>>> ShowRouseUser([FromQuery] RouseUserShowCommand request)
        {
            return mediator.Send(request);

        }
        /// <summary>
        /// 根据部门id查询用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<User>>> GetUseDepart([FromQuery] UserselectCommand request)
        {
            
            return mediator.Send(request);
        }

        /// <summary>
        /// 角色查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<RoleModel>>> GetRole()
        {
            var request = new GetRoleCommand();

            return mediator.Send(request);
        }


        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<UserDto>>> GetUser()
        {
            var request = new GetUsercommadn();
           
           
            return mediator.Send(request);

        }

        /// <summary>
        /// 获取省份信息
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public Task<ApiResult<List<Province>>> GetProvince()
        {
            var request = new GetProvincecommand();
            return mediator.Send(request);

        }
        /// <summary>
        /// 获取市信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<City>>> GetCity([FromQuery] GetCityCommand request)
        {
            return mediator.Send(request);

        }

        /// <summary>
        /// 获取县信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public Task<ApiResult<List<County>>> GetCounty([FromQuery] GetCountrycommand request)
        {
            return mediator.Send(request);

        }

      
        /// <summary>
        /// 获取用户角色信息表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]

        public Task<ApiResult<List<UserDto>>> UserRoleshow()
        {

            var resquest = new UserShowsCommand();
            return mediator.Send(resquest);

        }
       


        /// <summary>
        /// 角色菜单表保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> AddRoleMenu(RoleMenuCommand request)
        {
            return mediator.Send(request);

        }
        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// 

        [HttpPost]
        public Task<ApiResult<int>> DelMenu(DelMenuCommand request)
        { 
        
                return mediator.Send(request);

            
        }
        /// <summary>
        /// 修改菜单信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> UpdataMenus(UpdataMenusCommand request)
        { 
            return mediator.Send(request);
        
        }
        /// <summary>
        /// 菜单信息添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> Handle(AddMenuCommand request)
        { 
            return mediator.Send(request);
        
        }
        /// <summary>
        /// 删除角色用户表的信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> DelRoleUser(DelRoleUserCommand request)
        {
            return mediator.Send(request);
        
        }
        /// <summary>
        /// 给角色添加用户
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> AddRoleUser(AddRoleUserCommand request, CancellationToken cancellationToken)
        { 
            return mediator.Send(request);
        
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> AddRole(AddRoleCommand request)
        {
            return mediator.Send(request);
        
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> UpdateRole(UpdateRoleCommand request)
        {

            return mediator.Send(request);
        }
        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public Task<ApiResult<int>> DelRole(DelRoleCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public Task<ApiResult<int>> AddUser(AddUserCommand request)
        {
          

            return mediator.Send(request);
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpPost]
        public Task<ApiResult<int>> UpdataUser(UpdataUser request)
        {

            return mediator.Send(request);
        }
        /// <summary>
        /// 用户信息删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> DelUser(DelUserCommand request)
        {
            return mediator.Send(request);
        
        }
        
        /// <summary>
        /// 根据用户Id获取用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<List<User>>> GetUserName(List<int> indes)
        {
            var quest = new GetUserNameCommand();
            quest.userids = indes;
            return mediator.Send(quest);
        }


    }
}
